package applet.molecule;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;

public class CoordAxe {	
	private boolean isVisible;

	private float axisRadius;
	private float axisLen;
	
	private Color3f xColor;
	private Color3f yColor;
	private Color3f zColor;

	private float pointerRadius;
	private float pointerHeight;

	public CoordAxe(boolean isVisible, float axisRadius) {
		this.isVisible = isVisible;
		
		xColor = new Color3f(0.0f, 0.1f, 0.1f);
		yColor = new Color3f(0.4f, 0.4f, 0.0f);
		zColor = new Color3f(0.4f, 0.0f, 0.4f);

		this.axisRadius = axisRadius;
		axisLen = this.axisRadius*130;
		
		pointerRadius = this.axisRadius * 2f;
		pointerHeight = axisLen * 0.05f;
	}

	public TransformGroup getAxe(Color3f axisColor) {
		Appearance app = new Appearance();
		app.setMaterial(new Material(axisColor, axisColor, axisColor, axisColor,
				20.0f));

		Cylinder line = new Cylinder(axisRadius, axisLen, app);
		TransformGroup axis = new TransformGroup();
		axis.addChild(line);

		TransformGroup pointerGroup = new TransformGroup();
		Cone pointer = new Cone(pointerRadius, pointerHeight, app);
		pointerGroup.addChild(pointer);
		Transform3D t3d = new Transform3D();
		t3d.setTranslation(new Vector3f(0.0f, axisLen / 2 + pointerHeight / 2,
				0.0f));
		pointerGroup.setTransform(t3d);
		axis.addChild(pointerGroup);

		return axis;
	}

	public BranchGroup getGeometry() {
		BranchGroup axes = new BranchGroup();

		if (isVisible) {
			TransformGroup xAxe = getAxe(xColor);
			TransformGroup yAxe = getAxe(yColor);
			TransformGroup zAxe = getAxe(zColor);

			Transform3D xTrans = new Transform3D();
			Transform3D yTrans = new Transform3D();
			Transform3D zTrans = new Transform3D();

			xTrans.rotZ(-1.57);
			//yTrans.rotY(1.57);
			zTrans.rotX(1.57);

			xAxe.setTransform(xTrans);
			yAxe.setTransform(yTrans);
			zAxe.setTransform(zTrans);

			axes.addChild(xAxe);
			axes.addChild(yAxe);
			axes.addChild(zAxe);

			return axes;
		}

		return null;
	}
}
